package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.mes.vo.list.BomListVo;
import com.ruoyi.mes.vo.product.BomMtrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.ProBom;
import com.ruoyi.mes.service.IProBomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品BOMController
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Api(tags = "产品BOM")
@RestController
@RequestMapping("/mes/bom")
public class ProBomController extends BaseController {
    @Autowired
    private IProBomService proBomService;

    /**
     * 查询产品BOM列表
     */
    @ApiOperation(value = "查询产品BOM列表")
    @PreAuthorize("@ss.hasPermi('mes:bom:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProBom proBom) {
        startPage();
        List<ProBom> list = proBomService.selectProBomList(proBom);
        return getDataTable(list);
    }

    /**
     * 导出产品BOM列表
     */
    @ApiOperation(value = "导出产品BOM列表")
    @PreAuthorize("@ss.hasPermi('mes:bom:export')")
    @Log(title = "产品BOM", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProBom proBom) {
        List<ProBom> list = proBomService.selectProBomList(proBom);
        ExcelUtil<ProBom> util = new ExcelUtil<ProBom>(ProBom.class);
        util.exportExcel(response, list, "产品BOM数据");
    }

    /**
     * 获取产品BOM详细信息
     */
    @ApiOperation(value = "获取产品BOM详细信息")
    @PreAuthorize("@ss.hasPermi('mes:bom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proBomService.selectProBomById(id));
    }

    /**
     * 新增产品BOM
     */
    @PreAuthorize("@ss.hasPermi('mes:bom:add')")
    @Log(title = "产品BOM", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProBom proBom) {
        return toAjax(proBomService.insertProBom(proBom));
    }

    /**
     * 修改产品Bom
     */
    @ApiOperation(value = "修改产品BOM")
    @PreAuthorize("@ss.hasPermi('mes:bom:edit')")
    @Log(title = "产品BOM", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProBom proBom) {
        return toAjax(proBomService.updateProBom(proBom));
    }

    /**
     * 删除产品BOM
     */
    @ApiOperation(value = "删除产品BOM")
    @PreAuthorize("@ss.hasPermi('mes:bom:remove')")
    @Log(title = "产品BOM", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(proBomService.deleteProBomByIds(ids));
    }

    //*******************************************************

    /**
     * 查-Bom分页
     * @param proBom
     * @return
     */
    @ApiOperation(value = "查询Bom列表信息")
    @GetMapping("/bomList")
    public TableDataInfo bomList(ProBom proBom) {
        startPage();
        List<BomListVo> list = proBomService.selectBomListVo(proBom);
        return getDataTable(list);
    }

    /**
     * 增-Bom
     * @param bomMtrVo
     * @return
     */
    @ApiOperation(value = "新增Bom")
    @PostMapping("/saveBom")
    public AjaxResult saveBom(@RequestBody BomMtrVo bomMtrVo){
        return proBomService.saveBom(bomMtrVo);
    }

    /**
     * 改-Bom
     * @param bomMtrVo
     * @return
     */
    @PutMapping("/editBom")
    public AjaxResult editBom(@RequestBody BomMtrVo bomMtrVo){
        return proBomService.editBom(bomMtrVo);
    }

    /**
     * 删-Bom-Bom编码
     * @param bomCode
     * @return
     */
    @DeleteMapping("/deleteByBomCode")
    public AjaxResult deleteByBomCode(@RequestParam String bomCode){
        return proBomService.deleteByBomCode(bomCode);
    }

    /**
     * 增 - Bom - 产品是否有Bom
     * @param bomMtrVo
     * @return
     */
    @PostMapping("/addBom")
    public AjaxResult addBom(@RequestBody BomMtrVo bomMtrVo){
        return proBomService.addBom(bomMtrVo);
    }

    /**
     * 查 - Bom详情 - Bomid
     * @param id
     * @return
     */
    @GetMapping("/getDetails")
    public AjaxResult getDetails(@RequestParam Long id){
        return success(proBomService.getDetails(id));
    }
}
